<template>
  <div style="height: 100%">
    <el-form label-width="80px" label-position="right">
      <el-row :gutter="8">
        <el-col :span="8">
          <el-form-item label="数据存储">
            <el-select  v-model="entityInfo.callDb" @change="refreshTable" style="width: 100%">
              <template v-for="option in appContext.envs">
                <el-option v-if="option.envType === 'db'" :key="option.envProp" :value="option.envValue" :label="option.envLabel"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文件存储">
            <el-select  v-model="entityInfo.callStore" @change="refreshTable" style="width: 100%">
              <template v-for="option in appContext.envs">
                <el-option v-if="option.envType === 'store'" :key="option.envProp" :value="option.envValue" :label="option.envLabel"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-upload
              :action="uploadUrl"
              :headers="headers"
              :on-success="uploadSuccess"
              :show-file-list="false"
          >
            <el-button type="primary" icon="el-icon-upload2">点击上传</el-button>
          </el-upload>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="dataSource" border highlight-current-row @row-click="handleRowClick" @row-contextmenu="rightClick" :height="autoHeight">
      <el-table-column prop="resourceName" label="名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="entityType" label="实体类型" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="entityValue" label="实体值" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="storeUrl" label="存储地址" align="center" show-overflow-tooltip>
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="resourceSuffix" label="类型" align="center" width="100px">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" placeholder="过滤" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="resourceSize" label="大小" align="center" width="100px">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" placeholder="过滤" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="updateAt" label="更新日期" align="center" width="150px"></el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "AppStoreTable",
  mixins: [IdeaTable],
  inject: {
    appContext: {
      from: 'appContext',
      default: function () {
        return {}
      }
    }
  },
  props: {
    select: {
      type: Boolean,
      default: false
    },
    config: {
      type: Object,
      default: function () {
        return { storeId: '0' }
      }
    }
  },
  data() {
    return {
    }
  },
  computed: {
    autoHeight() {
      return `calc(100vh - 440px)`
    },
    uploadUrl () {
      return this.appendUrl(this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/upload'))
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    },
    perviewUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/preview/')
    },
    downLoadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/download/')
    }
  },
  created() {
    this.queryUrl = '/resource/queryPage'
    this.rowKey = 'resourceId'
    this.rightButtons.splice(0, this.rightButtons.length)
    this.appendRightBtn({ label: '预览', icon: 'el-icon-view', click: this.perview })
    this.appendRightBtn({ label: '下载', icon: 'el-icon-download', click: this.download })
    this.appendRightBtn({ label: '删除', icon: 'el-icon-delete-solid', click: this.remove })
    this.initEntity()
    this.refreshTable()
  },
  methods: {
    initEntity(){
      const { envs } = this.appContext
      envs.forEach(item => {
        if (item.envType === 'db') {
          this.$set(this.entityInfo, 'callDb', item.envValue)
        } else if (item.envType === 'store') {
          this.$set(this.entityInfo, 'callStore', item.envValue)
        }
      })
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        this.refreshTable()
      }
    },
    perview (row) {
      if (row.storeUrl && (row.storeUrl.includes('http://') || row.storeUrl.includes('https://'))) {
        window.open(row.storeUrl)
      } else if (row.resourceId) {
        const url = this.appendUrl(this.perviewUrl + row.resourceId)
        window.open(url + '&access_token=' + this.$AuthStorage.getAccessToken())
      }
    },
    download (row) {
      if (row.storeUrl && (row.storeUrl.includes('http://') || row.storeUrl.includes('https://'))) {
        window.open(row.storeUrl)
      } else if (row.resourceId) {
        const url = this.appendUrl(this.downLoadUrl + row.resourceId)
        window.open(url + '&access_token=' + this.$AuthStorage.getAccessToken())
      }
    },
    remove (row) {
      if (row.resourceId) {
        const url = this.appendUrl( 'resource/delete/' + row.resourceId)
        this.$remove(url)
            .then(res => {
              this.$resultMsg(this, res, '删除失败')
              if (res.code === 200) {
                this.loadSource()
              }
            })
      }
    },
    refreshTable() {
      this.addFilter({field: 'storeId',op:'=',value: this.entityInfo.callStore })
      this.loadSource()
    },
    appendUrl(url) {
      return url + '?appId=' + this.appInfo.appId + '&callDb=' + this.entityInfo.callDb + "&callStore=" + this.entityInfo.callStore
    }
  }
}
</script>

<style scoped>

</style>
